﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _020_021_022事件详解_7事件简化声明
{
    internal class Program
    {
        static void Main(string[] args)
        {

            Customer customer = new Customer();
            waitor waitor = new waitor();
            customer.Order += waitor.Action;
            //customer.Action();
            Console.ReadLine(); 
            OrderEventArgs e=new OrderEventArgs();
            e.DishName = "满汉全席";
            e.Size = "large";

            OrderEventArgs e2 = new OrderEventArgs();
            e2.DishName = "全坤套餐";
            e2.Size = "large";

            Customer badGuy = new Customer();
            badGuy.Order += waitor.Action;

            badGuy.Order.Invoke(customer,e);
            badGuy.Order.Invoke(customer,e2);
            customer.PayBill();
        }
    }

    public delegate void OrderEventHandler(Customer customer, OrderEventArgs order);

    public class Customer
    {
        //简化事件声明
        //public event OrderEventHandler Order

        //此时非事件  变成委托  安全性低  可以随意访问
        public  OrderEventHandler Order;

        public double Bill { get; set; }

        public void PayBill()
        {
            Console.WriteLine("I Will pay the bill ${0}", this.Bill);
        }

        public void SetOff()
        {
            Console.WriteLine("Go to the restaurant");
        }
        public void SitDown()
        {
            Console.WriteLine("Sit Down");
        }

        public void Think()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Let me think");
                Thread.Sleep(1000);
            }
            if (this.Order != null)
            {
                OrderEventArgs orderEventArgs = new OrderEventArgs();
                orderEventArgs.DishName = "Chicken";
                orderEventArgs.Size = "large";
                //触发委托事件  发布事件  
                this.Order.Invoke(this, orderEventArgs);
            }
        }

        public void Action()
        {
            Console.ReadLine();
            this.SetOff();
            this.SitDown();
            this.Think();
        }
    }

    public class OrderEventArgs : EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    public class waitor
    {
        internal void Action(Customer customer, OrderEventArgs e)
        {
            Console.WriteLine("I Will Serve for you");
            double price = 10;
            switch (e.Size)
            {
                case "large":
                    price *= 2;
                    break;
                case "small":
                    price *= 1.5;
                    break;
                default:
                    break;
            }
            customer.Bill += price;
        }
    }
}
